package com.zhku.onlineshop.Dao;

import com.zhku.onlineshop.Pojo.CartItem;
import com.zhku.onlineshop.dto.CartItem_Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CartDao {
    //查看购物车是否存在该商品
    @Select("SELECT *FROM cart_items WHERE user_id = #{user_id} AND product_id=#{product_id}")
    CartItem findByUserIdAndProductId(@Param("user_id") Integer user_id, @Param("product_id") Integer product_id);
    //购物车不存在该商品，添加一行
    @Insert("INSERT INTO cart_items (user_id,product_id,quantity) values (#{user_id},#{product_id},1)")
    boolean AddIntoCart(@Param("user_id") Integer user_id, @Param("product_id") Integer product_id);
    //购物车存在该商品，增加数量
    @Update("UPDATE cart_items SET quantity=quantity+1 WHERE user_id=#{user_id} AND product_id=#{product_id}")
    boolean updateCart(@Param("user_id") Integer userId, @Param("product_id") Integer productId);

    @Select("SELECT cart_items.product_id,cart_items.quantity,products.shop_id,products.shop_name,products.shop_price,products.shop_description,products.shop_img FROM cart_items LEFT JOIN products ON cart_items.product_id=products.id WHERE user_id = #{user_id}")
    List<CartItem_Product> GetCartByID(@Param("user_id") Integer userId);

    @Update("UPDATE cart_items SET quantity=quantity-1 WHERE user_id=#{user_id} AND product_id=#{product_id}")
    boolean ReduceProductNum(@Param("user_id") Integer userId, @Param("product_id") Integer productId);

    @Delete("DELETE FROM  cart_items WHERE user_id=#{user_id} AND product_id=#{product_id}")
    boolean DelProduct(@Param("user_id") Integer userId, @Param("product_id") Integer productId);
}
